#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class TeachingPlanProvider : CY.GFive.Core.Providers.DALProvider.ITeachingPlanProvider
	{
		#region Sql
		private static readonly string SqlSelectTeachingPlan = "SELECT [Id], [ProCode],[CourseCode],[Term],[CourseCate],[IsTheory],[CrsNumPerTerm] FROM [TeachingPlan] WHERE  [Id]=@Id";

		private static readonly string SqlInsertTeachingPlan = "INSERT INTO [TeachingPlan]([ProCode],[CourseCode],[Term],[CourseCate],[IsTheory],[CrsNumPerTerm]) values(@ProCode,@CourseCode,@Term,@CourseCate,@IsTheory,@CrsNumPerTerm);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateTeachingPlan = "UPDATE [TeachingPlan] SET [ProCode] = @ProCode,[CourseCode] = @CourseCode,[Term] = @Term,[CourseCate] = @CourseCate,[IsTheory] = @IsTheory,[CrsNumPerTerm] = @CrsNumPerTerm WHERE  [Id]=@Id";

		private static readonly string SqlDeleteTeachingPlan = "DELETE [TeachingPlan] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllTeachingPlan = "SELECT [Id], [ProCode],[CourseCode],[Term],[CourseCate],[IsTheory],[CrsNumPerTerm] FROM [TeachingPlan]";
		#endregion

		#region ITeachingPlanProvider Members
		public Core.Business.TeachingPlan Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectTeachingPlan);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.TeachingPlan teachingPlan = new Core.Business.TeachingPlan();

				if (!reader.IsDBNull(0)) teachingPlan.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) teachingPlan.ProCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) teachingPlan.CourseCode = reader.GetString(2);
				if (!reader.IsDBNull(3)) teachingPlan.Term = reader.GetInt32(3);
				if (!reader.IsDBNull(4)) teachingPlan.CourseCate = reader.GetString(4);
				if (!reader.IsDBNull(5)) teachingPlan.IsTheory = reader.GetBoolean(5);
				if (!reader.IsDBNull(6)) teachingPlan.CrsNumPerTerm = reader.GetInt32(6);

				return teachingPlan;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.TeachingPlan teachingPlan)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@ProCode", SqlDbType.NVarChar, teachingPlan.ProCode);
			sql.AddParameter("@CourseCode", SqlDbType.NVarChar, teachingPlan.CourseCode);
			sql.AddParameter("@Term", SqlDbType.Int, teachingPlan.Term);
			sql.AddParameter("@CourseCate", SqlDbType.NVarChar, teachingPlan.CourseCate);
			sql.AddParameter("@IsTheory", SqlDbType.Bit, teachingPlan.IsTheory);
			sql.AddParameter("@CrsNumPerTerm", SqlDbType.Int, teachingPlan.CrsNumPerTerm);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertTeachingPlan);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) teachingPlan.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.TeachingPlan teachingPlan)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@ProCode", SqlDbType.NVarChar, teachingPlan.ProCode);
			sql.AddParameter("@CourseCode", SqlDbType.NVarChar, teachingPlan.CourseCode);
			sql.AddParameter("@Term", SqlDbType.Int, teachingPlan.Term);
			sql.AddParameter("@CourseCate", SqlDbType.NVarChar, teachingPlan.CourseCate);
			sql.AddParameter("@IsTheory", SqlDbType.Bit, teachingPlan.IsTheory);
			sql.AddParameter("@CrsNumPerTerm", SqlDbType.Int, teachingPlan.CrsNumPerTerm);
			sql.AddParameter("@Id", SqlDbType.Int, teachingPlan.Id);
			sql.ExecuteSql(SqlUpdateTeachingPlan);
		}

		public void Delete(Core.Business.TeachingPlan teachingPlan)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, teachingPlan.Id);
			sql.ExecuteSql(SqlDeleteTeachingPlan);
		}

		public IList<Core.Business.TeachingPlan> GetAllTeachingPlan()
		{
			IList<Core.Business.TeachingPlan> teachingPlanlist = new List<Core.Business.TeachingPlan>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllTeachingPlan);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.TeachingPlan teachingPlan = new Core.Business.TeachingPlan();

					if (!reader.IsDBNull(0)) teachingPlan.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) teachingPlan.ProCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) teachingPlan.CourseCode = reader.GetString(2);
					if (!reader.IsDBNull(3)) teachingPlan.Term = reader.GetInt32(3);
					if (!reader.IsDBNull(4)) teachingPlan.CourseCate = reader.GetString(4);
					if (!reader.IsDBNull(5)) teachingPlan.IsTheory = reader.GetBoolean(5);
					if (!reader.IsDBNull(6)) teachingPlan.CrsNumPerTerm = reader.GetInt32(6);

					teachingPlan.MarkOld();
					teachingPlanlist.Add(teachingPlan);
				}
				reader.Close();
			}
			return teachingPlanlist;
		}
		#endregion
	}
}
